﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Globalization;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class hearing : System.Web.UI.Page
    {
        public static List<string> x = new List<string>();
        DataTable dt = new DataTable();
        DataTable dx = new DataTable();
      
        protected void Page_Load(object sender, EventArgs e)
        {


           
            if (!IsPostBack)
            {
                BindData();
       
            }
            if (Session["CaseNo"] == null)
            {

                MessageBox("กรุณากดค้นหา หรือลงทะเบียนการคัดกรองเด็กก่อน", "checkcid.aspx");

            }


        }

        private void MessageBox(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }


        protected void BindData()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            
            //dx.Clear();
            x.Clear();
            scon.Open();
            SqlCommand cmd = new SqlCommand("select Name from HearProblem", scon);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                x.Add(reader["Name"].ToString());
            }
            reader.Close();
            dx.Columns.Add("Name");

            for (int i = 0; i < x.Count(); i++)
            {
                dx.Rows.Add();
                dx.Rows[i]["Name"] = x[i].ToString();
            }
            GridView1.DataSource = dx;
            GridView1.DataBind();
            scon.Close();


            BindDataread2();
        }


        protected void Button1_Click(object sender, EventArgs e)
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            if (dt.Columns.Count <= 0)
            {
                dt.Columns.Add("CID");
                dt.Columns.Add("CaseNo");
                dt.Columns.Add("HearEvalCode");
                dt.Columns.Add("RecorderID");
                dt.Columns.Add("EvalDateTime");
                dt.Columns.Add("CMPlanNo");
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                scon.Open();
                SqlCommand cmd = new SqlCommand("select HearEvalCode from HearProblem where Name = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", GridView1.Rows[row.RowIndex].Cells[0].Text);

                CheckBox cb = (CheckBox)row.FindControl("CBX");
                if (cb != null && cb.Checked)
                {
                    SqlDataReader reader2 = cmd.ExecuteReader();
                    if (reader2.Read())
                    {
                        dt.Rows.Add(Session["CID"], Session["CaseNo"], reader2["HearEvalCode"].ToString(), Session["Staff_id"], DateTime.Now.ToString(), Session["CMPlanNo"]);
                    }
                    else
                    {
                    }
                }
                scon.Close();
            }
            //GridView2.DataSource = dt;
            //GridView2.DataBind();


            SqlCommand command2 = new SqlCommand("delete from [HearEvaluation] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", Session["CID"]);
            command2.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            command2.ExecuteNonQuery();
            scon.Close();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                SqlCommand command = new SqlCommand("INSERT INTO [HearEvaluation] (CID,CaseNo,HearEvalCode,RecorderID,EvalDateTime,CMPlanNo) VALUES('" + dt.Rows[i]["CID"].ToString() + "','" + dt.Rows[i]["CaseNo"].ToString() + "','" + dt.Rows[i]["HearEvalCode"].ToString() + "','" + dt.Rows[i]["RecorderID"].ToString() + "','" + dt.Rows[i]["EvalDateTime"].ToString() + "','" + dt.Rows[i]["CMPlanNo"].ToString() + "')", scon);
                scon.Open();

                command.ExecuteNonQuery();
                scon.Close();
            }



           // crepsnumber();
         //   psnumbertolabel();
          //  creDetails();
        }











       




        public void BindDataread2()
        {



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dthear = new DataTable();


            SqlCommand command = new SqlCommand("select b.Name as HearEvalCode from [HearEvaluation] a join HearProblem b on a.HearEvalCode=b.HearEvalCode where CID=@CID and CaseNo=@CaseNo", scon);

            command.Parameters.AddWithValue("@CID", Session["CID"]);
            command.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            command.ExecuteNonQuery();




            SqlDataReader reader = command.ExecuteReader();

            dthear.Columns.Add("HearEvalCode");

            while (reader.Read() != false)
            {
                dthear.Rows.Add(reader["HearEvalCode"].ToString());
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            reader.Close();


            for (int j = 0; j < dthear.Rows.Count; j++)
            {
                string a = dthear.Rows[j]["HearEvalCode"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CBX");

                    if (a == b)
                    {

                        //tb.Text = dthear2.Rows[j]["Note"].ToString();
                        // tb.Text=
                        chk.Checked = true;
                    }
                }
            }


            scon.Close();







        }


      
    
    }




}